package com.baili.controller.admin;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.baili.dto.BaseTransferEntity;
import com.baili.dto.Defect;
import com.baili.dto.SelectUserInput;
import com.baili.service.admin.AdminService;
import com.baili.util.EhcacheUtil;
import com.baili.util.MobileConfig;
 
/**
 * @author iceX
 * @version 创建时间：2016年3月25日 上午9:59:58
 * @Name: CallController.java 系统管理
 */
@Controller
@RequestMapping(value = "")
public class AdminController {
	private static Logger log = Logger.getLogger(AdminController.class);

	@Autowired
	private AdminService adminService;
	@Autowired
	private EhcacheUtil ehcacheUtil;

	/**
	 * 系统管理员 审批统计
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/adminApCount")
	@ResponseBody
	public BaseTransferEntity adminApCount(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {

			
			// 获取列表数据
			List lst = adminService.adminApCount();

			// 返回列表数据
			if (lst == null || lst.size() < 1) {
				//baseTransferEntity.setTotalCount("");
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.error.list"));
				baseTransferEntity.setData(null);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.error.list"));
				return baseTransferEntity;
			} else {
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.success"));
				baseTransferEntity.setData(lst);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.success"));
			}
		} catch (Exception e) {
			log.error("--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}

		return baseTransferEntity;
	}

	/**
	 * 系统管理员 已审、未审列表
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/adminApList")
	@ResponseBody
	public BaseTransferEntity adminApList(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {

			int approve = Integer.parseInt(request.getParameter("approve"));
			// 获取列表数据
			List lst = adminService.adminApList(approve);

			// 返回列表数据
			if (lst == null || lst.size() < 1) {
				//baseTransferEntity.setTotalCount("");
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.error.list"));
				baseTransferEntity.setData(null);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.error.list"));
				return baseTransferEntity;
			} else {
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.success"));
				baseTransferEntity.setData(lst);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.success"));
			}
		} catch (Exception e) {
			log.error("--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}

		return baseTransferEntity;
	}
	
	/**
	 * 系统管理员 根据id查看已审、未审人员详情
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/adminApById")
	@ResponseBody
	public BaseTransferEntity adminApById(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			
			int id = Integer.parseInt(request.getParameter("id"));
			// 获取列表数据
			List lst = adminService.adminApById(id);
			
			// 返回列表数据
			if (lst == null || lst.size() < 1) {
				//baseTransferEntity.setTotalCount("");
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.error.list"));
				baseTransferEntity.setData(null);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.error.list"));
				return baseTransferEntity;
			} else {
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.success"));
				baseTransferEntity.setData(lst);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.success"));
			}
		} catch (Exception e) {
			log.error("--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
		
		return baseTransferEntity;
	}
	
	/**
	 * 系统管理员 根据人员类型显示生产小组或者工序(下拉列表)
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/adminApByRoleName")
	@ResponseBody
	public BaseTransferEntity adminApByRoleName(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			
			String roleName = request.getParameter("roleName");
			
			SelectUserInput user = new SelectUserInput();
			user.setName(roleName);
			// 获取列表数据
			List lst = adminService.adminApByRoleName(user);
			
			// 返回列表数据
			if (lst == null || lst.size() < 1) {
				//baseTransferEntity.setTotalCount("");
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.error.list"));
				baseTransferEntity.setData(null);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.error.list"));
				return baseTransferEntity;
			} else {
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.success"));
				baseTransferEntity.setData(lst);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.success"));
			}
		} catch (Exception e) {
			log.error("--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
		
		return baseTransferEntity;
	}
	
	/**
	 * 系统管理员 批准驳回申请人员操作
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/updateApState")
	@ResponseBody
	public BaseTransferEntity updateApState(HttpServletRequest request,
			HttpServletResponse response) {
		
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		try {
			
			int approve = Integer.parseInt(request.getParameter("approve"));
			String apId = request.getParameter("apId");
			
			String productTeamId = request.getParameter("productTeamId");
			String processId = request.getParameter("processId");
			
			String roleType = request.getParameter("roleType");
			
			SelectUserInput user = new SelectUserInput();
			
			if(roleType.equals("O") || roleType.equals("M")){
				
				user.setProductTeamId(Integer.parseInt(productTeamId));
				
				user.setProcessId(0);
				
			}else if(roleType.equals("P") || roleType.equals("F") || roleType.equals("T")){
				
				user.setProductTeamId(0);
				
				user.setProcessId(Integer.parseInt(processId));
			}else{
				
				user.setProductTeamId(0);
				
				user.setProcessId(0);
			}
			
			user.setApprove(approve);
			
			user.setId(Integer.parseInt(apId));
			
			user.setCheckTime(sdf.format(new Date()));
			
			adminService.updateApState(user);
			
			// 返回列表数据
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData("");
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));
		} catch (Exception e) {
			log.error("--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
		
		return baseTransferEntity;
	}
	
	/**
	 * 系统管理员 呼叫设置 通过姓名、手机号查询呼叫负责人
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/getAdminName")
	@ResponseBody
	public BaseTransferEntity getAdminName(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			
			String realName = request.getParameter("realName");
			String mobile = request.getParameter("mobile");
			
			SelectUserInput user = new SelectUserInput();
			user.setName(realName);
			user.setMobile(mobile);
			
			// 获取列表数据
			List lst = adminService.getAdminName(user);
			
			// 返回列表数据
			if (lst == null || lst.size() < 1) {
				//baseTransferEntity.setTotalCount("");
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.error.list"));
				baseTransferEntity.setData(null);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.error.list"));
				return baseTransferEntity;
			} else {
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.success"));
				baseTransferEntity.setData(lst);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.success"));
			}
		} catch (Exception e) {
			log.error("--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
		
		return baseTransferEntity;
	}
	/**
	 * 系统管理员 呼叫设置 通过姓名、手机号查询呼叫负责人
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/getProcess")
	@ResponseBody
	public BaseTransferEntity getProcess(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			
			// 获取列表数据
			List lst = adminService.getProcess();
			
			// 返回列表数据
			if (lst == null || lst.size() < 1) {
				//baseTransferEntity.setTotalCount("");
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.error.list"));
				baseTransferEntity.setData(null);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.error.list"));
				return baseTransferEntity;
			} else {
				baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.success"));
				baseTransferEntity.setData(lst);
				baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.success"));
			}
		} catch (Exception e) {
			log.error("--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
		
		return baseTransferEntity;
	}
	
	/**
	 * 系统管理员 上传文件
	 * @param request
	 * @param response
	 * @return 
	 */
	@ResponseBody
	@RequestMapping(value = "/service/uploadfile")
	public BaseTransferEntity uploadfile (@RequestParam("uploadfile")CommonsMultipartFile file ,
			HttpServletRequest request,HttpServletResponse response) throws IOException {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
		String picId = request.getParameter("picId").toString();
		String userId = request.getParameter("userId").toString();
		
		Object object = ehcacheUtil.getCacheElement(userId);
		if(file != null ){
			
			String type = file.getOriginalFilename().substring(file.getOriginalFilename().indexOf("."));
			
			InputStream fil = file.getInputStream();
			
			if (object == null) {
				
				Map<String,Object> map = new HashMap<String,Object>();
				
				map.put(picId, fil);
				
				map.put(picId+"type", type);
				
				ehcacheUtil.addToCache(userId, map);
				
			}else{
				
				Map<String,Object> map = (Map<String,Object>)ehcacheUtil.getCacheElement(userId);
				
				map.put(picId, fil);
				
				map.put(picId+"type", type);
			}
		}
		
		// MultipartFile是对当前上传的文件的封装，当要同时上传多个文件时，可以给定多个MultipartFile参数(数组)
//		if (!file.isEmpty()) {
//				
//			adminService.uploadfile(file);
//		}	
		baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.success"));
		baseTransferEntity.setData("");
		baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.success"));
		} catch (Exception e) {
			log.error("--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
		return baseTransferEntity;
	}
	
	/**
	 * 系统管理员 批量上传文件
	 * @param request
	 * @param response
	 * @return 
	 */
	@RequestMapping(value = "/service/uploadfiles")
	public BaseTransferEntity uploadfiles (@RequestParam("uploadfile")CommonsMultipartFile[] files ,
			HttpServletRequest request) throws IOException {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
		String picId = request.getParameter("picId").toString();
		String userId = request.getParameter("userId").toString();
		
		Object object = ehcacheUtil.getCacheElement(userId);
		if(files != null ){
			
			String type = files[0].getOriginalFilename().substring(files[0].getOriginalFilename().indexOf("."));
			
			InputStream fil = files[0].getInputStream();
			
			if (object == null) {
				
				Map<String,Object> map = new HashMap<String,Object>();
				
				map.put(picId+"."+type, fil);
				
				map.put(picId+type, type);
				
				ehcacheUtil.addToCache(userId, map);
			}else{
				
				Map<String,Object> map = (Map<String,Object>)ehcacheUtil.getCacheElement(userId);
				
				map.put(picId+"."+type, fil);
				
				map.put(picId+type, type);
				
			}
			
			
			type = files[1].getOriginalFilename().substring(files[1].getOriginalFilename().indexOf("."));
			
			fil = files[1].getInputStream();
			
			if (object == null) {
				
				Map<String,Object> map = new HashMap<String,Object>();
				
				map.put(picId+"."+type, fil);
				
				map.put(picId+type, type);
				
				ehcacheUtil.addToCache(userId, map);
			}else{
				
				Map<String,Object> map = (Map<String,Object>)ehcacheUtil.getCacheElement(userId);
				
				map.put(picId+"."+type, fil);
				
				map.put(picId+type, type);
			}
		}	
		
		baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
		baseTransferEntity.setData("");
		baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.success"));
		} catch (Exception e) {
			log.error("--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
		return baseTransferEntity;
	}
	
	
	/**
	 * 质检设置
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/uploadQualityCheck")
	@ResponseBody
	public BaseTransferEntity uploadQualityCheck(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			
			String processId = request.getParameter("processId");
			String defectDetails = request.getParameter("defectDetails");
			
			Defect defect = new Defect();
			
			defect.setProcessId(Integer.parseInt(processId));
			
			defect.setComment(defectDetails);
			
			adminService.uploadQualityCheck(defect);
			
			baseTransferEntity.setResultcode(MobileConfig
						.getStringCode("code.global.success"));
			baseTransferEntity.setData("");
			baseTransferEntity.setDesc(MobileConfig
						.getString("msg.global.success"));
		} catch (Exception e) {
			log.error("--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
		
		return baseTransferEntity;
	}
	
	/**
	 * 版本更新
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/getVersion")
	@ResponseBody
	public BaseTransferEntity getVersion(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			
			List lst = adminService.getVersion();
			
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData(lst);
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));
		} catch (Exception e) {
			log.error("--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
		
		return baseTransferEntity;
	}

	/**
	 * 向callType表中添加数据
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/callSet")
	@ResponseBody
	public BaseTransferEntity callSet(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			
			String processId = request.getParameter("processId");
			
			String charge = request.getParameter("charge");
			
			String callType = request.getParameter("callType");
			
			adminService.callSet(callType,Integer.parseInt(processId),Integer.parseInt(charge));
			
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData("");
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));
		} catch (Exception e) {
			log.error("--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
		
		return baseTransferEntity;
	}
	/**
	 * 向callType表中添加数据
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/defectSet")
	@ResponseBody
	public BaseTransferEntity defectSet(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			String processId = request.getParameter("processId");
			
			String defectName = request.getParameter("defectName");
			
			adminService.defectSet(defectName,Integer.parseInt(processId));
			
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData("");
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));
		} catch (Exception e) {
			log.error("--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
		
		return baseTransferEntity;
	}
	/**
	 * 向callType表中添加数据
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/insertSize")
	@ResponseBody
	public BaseTransferEntity insertSize(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			String size = request.getParameter("size");
			
			String sex = request.getParameter("sex");
			
			adminService.insertSize(Integer.parseInt(size),Integer.parseInt(sex));
			
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData("");
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));
		} catch (Exception e) {
			log.error("--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
		
		return baseTransferEntity;
	}
	/**
	 * 向callType表中添加数据
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/deleteSize")
	@ResponseBody
	public BaseTransferEntity deleteSize(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			String sizeId = request.getParameter("sizeId");
			
			adminService.deleteSize(Integer.parseInt(sizeId));
			
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData("");
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));
		} catch (Exception e) {
			log.error("--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
		
		return baseTransferEntity;
	}
	
	/**
	 * 删除缺陷标题
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/service/deleteDefectType")
	@ResponseBody
	public BaseTransferEntity deleteDefectType(HttpServletRequest request,
			HttpServletResponse response) {
		BaseTransferEntity baseTransferEntity = new BaseTransferEntity();
		try {
			String defetcId = request.getParameter("defetcId");
			
			adminService.deleteDefectType(Integer.parseInt(defetcId));
			
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.success"));
			baseTransferEntity.setData("");
			baseTransferEntity.setDesc(MobileConfig
					.getString("msg.global.success"));
		} catch (Exception e) {
			log.error("--------->" + e.getMessage(), e);
			baseTransferEntity.setResultcode(MobileConfig
					.getStringCode("code.global.error.exception"));
			baseTransferEntity.setDesc("系统接口异常，请联系管理员！");
			baseTransferEntity.setData("");
		}
		
		return baseTransferEntity;
	}
}
